﻿using Dyao.Book.Application.Abstractions;
using Dyao.Book.Contract;
using Dyao.Book.Entity;
using Dyao.Book.Repository.Abstractions;
using Foundation.IdCreate;

namespace Dyao.Book.Application
{
    public class CommonManager : BaseManager, ICommonManager
    {
        /// <inheritdoc cref="ICommonManager.AddOperationLogsAsync"/>
        public async Task AddOperationLogsAsync(IEnumerable<OperationLogDto> logs, CancellationToken cancellationToken)
        {
            var idCreater = GetService<IIdCreater>();
            var logRepo = GetService<IOperationLogRepository>();

            var logEntities = ObjectMapper.Map<List<OperationLogDto>, List<OperationLogEntity>>(logs.ToList());

            logEntities.ForEach(async logEntity =>
            {
                var code = await idCreater.CreateAsync(IdBusinessType.OperationLogGuidId, null, cancellationToken);
                logEntity.ID = code.ToString();
                logEntity.CreateDateTime = DateTime.Now;
                logEntity.CreateUserName = CurrentUser.UserName;
            });

            await logRepo.BulkInsertAsync(logEntities, cancellationToken);
        }

        /// <inheritdoc cref="ICommonManager.AddOperationLogsAsync"/>
        public async Task AddOperationLogsAsync(params OperationLogDto[] logs)
        {
            await AddOperationLogsAsync(logs, default);
        }
    }
}
